<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css" rel="stylesheet">
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap-icons@1.8.0/font/bootstrap-icons.css">
    <style>
        :root {
            --primary-color: #4e73df;
            --secondary-color: #858796;
            --success-color: #1cc88a;
            --light-bg: #f8f9fc;
        }

        body {
            background: linear-gradient(135deg, #f5f7fa 0%, #c3cfe2 100%);
            height: 100vh;
            display: flex;
            align-items: center;
            font-family: 'Nunito', -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Helvetica Neue', Arial, sans-serif;
        }

        .login-container {
            max-width: 400px;
            width: 100%;
            margin: 0 auto;
        }

        .login-card {
            background-color: #fff;
            border-radius: 1rem;
            box-shadow: 0 0.15rem 1.75rem 0 rgba(58, 59, 69, 0.15);
            overflow: hidden;
        }

        .login-header {
            background: linear-gradient(180deg, var(--primary-color) 10%, #224abe 100%);
            color: white;
            padding: 2rem;
            text-align: center;
        }

        .login-body {
            padding: 2rem;
        }

        .form-control:focus {
            border-color: var(--primary-color);
            box-shadow: 0 0 0 0.2rem rgba(78, 115, 223, 0.25);
        }

        .input-group-text {
            background-color: #eaecf4;
            border: 1px solid #d1d3e2;
        }

        .btn-login {
            background: linear-gradient(180deg, var(--primary-color) 10%, #224abe 100%);
            border: none;
            padding: 0.75rem 1rem;
            font-weight: 600;
            transition: all 0.3s;
        }

        .btn-login:hover {
            background: linear-gradient(180deg, #3a59c7 10%, #1e3e9c 100%);
            transform: translateY(-2px);
        }

        .divider {
            position: relative;
            text-align: center;
            margin: 1.5rem 0;
        }

        .divider::before {
            content: "";
            position: absolute;
            top: 50%;
            left: 0;
            right: 0;
            border-top: 1px solid #d1d3e2;
        }

        .divider-text {
            position: relative;
            background-color: #fff;
            padding: 0 0.5rem;
            color: var(--secondary-color);
            font-size: 0.9rem;
        }

        .social-login .btn {
            border-radius: 50px;
            font-weight: 600;
        }

        .login-footer {
            text-align: center;
            padding: 1rem;
            background-color: var(--light-bg);
            font-size: 0.9rem;
        }

        .password-toggle {
            cursor: pointer;
            color: var(--secondary-color);
        }

        .password-toggle:hover {
            color: var(--primary-color);
        }

        .error-message {
            animation: fadeIn 0.5s;
        }

        @keyframes fadeIn {
            from { opacity: 0; }
            to { opacity: 1; }
        }
    </style>
</head>
<body>
    <div class="container">
        <div class="login-container">
            <div class="login-card">
                <div class="login-header">
                    <h2><i class="bi bi-shield-lock"></i> 用户登录</h2>
                    <p class="mb-0">欢迎回到我们的应用</p>
                </div>

                <div class="login-body">
                    <form id="loginForm" action="${pageContext.request.contextPath}/login" method="post">
                        <!-- 错误消息显示 -->
                        <c:if test="${not empty error}">
                            <div class="alert alert-danger error-message" role="alert">
                                <i class="bi bi-exclamation-circle"></i> ${error}
                            </div>
                        </c:if>

                        <!-- 成功消息显示 -->
                        <c:if test="${not empty message}">
                            <div class="alert alert-success error-message" role="alert">
                                <i class="bi bi-check-circle"></i> ${message}
                            </div>
                        </c:if>

                        <!-- 用户名输入 -->
                        <div class="mb-3">
                            <label for="username" class="form-label">用户名</label>
                            <div class="input-group">
                                <span class="input-group-text"><i class="bi bi-person"></i></span>
                                <input type="text" class="form-control" id="username" name="username"
                                       placeholder="请输入用户名" value="${param.username}" required autofocus>
                            </div>
                            <div class="form-text text-danger" id="usernameError"></div>
                        </div>

                        <!-- 密码输入 -->
                        <div class="mb-3">
                            <label for="password" class="form-label">密码</label>
                            <div class="input-group">
                                <span class="input-group-text"><i class="bi bi-key"></i></span>
                                <input type="password" class="form-control" id="password" name="password"
                                       placeholder="请输入密码" required>
                                <span class="input-group-text password-toggle" id="passwordToggle">
                                    <i class="bi bi-eye"></i>
                                </span>
                            </div>
                            <div class="form-text text-danger" id="passwordError"></div>
                        </div>

                        <!-- 记住我和忘记密码 -->
                        <div class="mb-3 d-flex justify-content-between align-items-center">
                            <div class="form-check">
                                <input class="form-check-input" type="checkbox" id="rememberMe" name="rememberMe">
                                <label class="form-check-label" for="rememberMe">记住我</label>
                            </div>
                            <a href="#" class="text-decoration-none">忘记密码?</a>
                        </div>

                        <!-- 提交按钮 -->
                        <button type="submit" class="btn btn-login w-100 mb-3">
                            <i class="bi bi-box-arrow-in-right"></i> 登录
                        </button>

                        <!-- 分隔线 -->
                        <div class="divider">
                            <span class="divider-text">或使用其他方式登录</span>
                        </div>

                        <!-- 社交登录 -->
                        <div class="social-login mb-3">
                            <div class="row g-2">
                                <div class="col-6">
                                    <a href="#" class="btn btn-outline-primary w-100">
                                        <i class="bi bi-google"></i> Google
                                    </a>
                                </div>
                                <div class="col-6">
                                    <a href="#" class="btn btn-outline-dark w-100">
                                        <i class="bi bi-github"></i> GitHub
                                    </a>
                                </div>
                            </div>
                        </div>
                    </form>
                </div>

                <div class="login-footer">
                    <p class="mb-0">还没有账号? <a href="${pageContext.request.contextPath}/register" class="text-decoration-none">立即注册</a></p>
                </div>
            </div>

            <div class="text-center mt-3">
                <p class="text-muted small">&copy; 2023 Spring MVC应用. 保留所有权利</p>
            </div>
        </div>
    </div>

    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/js/bootstrap.bundle.min.js"></script>
    <script>
        document.addEventListener('DOMContentLoaded', function() {
            const loginForm = document.getElementById('loginForm');
            const usernameInput = document.getElementById('username');
            const passwordInput = document.getElementById('password');
            const usernameError = document.getElementById('usernameError');
            const passwordError = document.getElementById('passwordError');
            const passwordToggle = document.getElementById('passwordToggle');

            // 密码显示/隐藏功能
            passwordToggle.addEventListener('click', function() {
                const type = passwordInput.getAttribute('type') === 'password' ? 'text' : 'password';
                passwordInput.setAttribute('type', type);

                // 切换图标
                const icon = this.querySelector('i');
                icon.classList.toggle('bi-eye');
                icon.classList.toggle('bi-eye-slash');
            });

            // 表单验证
            loginForm.addEventListener('submit', function(e) {
                let isValid = true;

                // 重置错误信息
                usernameError.textContent = '';
                passwordError.textContent = '';

                // 验证用户名
                if (!usernameInput.value.trim()) {
                    usernameError.textContent = '请输入用户名';
                    usernameInput.focus();
                    isValid = false;
                } else if (usernameInput.value.trim().length < 3) {
                    usernameError.textContent = '用户名至少需要3个字符';
                    usernameInput.focus();
                    isValid = false;
                }

                // 验证密码
                if (!passwordInput.value) {
                    passwordError.textContent = '请输入密码';
                    if (isValid) passwordInput.focus();
                    isValid = false;
                } else if (passwordInput.value.length < 6) {
                    passwordError.textContent = '密码至少需要6个字符';
                    if (isValid) passwordInput.focus();
                    isValid = false;
                }

                if (!isValid) {
                    e.preventDefault();
                } else {
                    // 显示加载状态
                    const submitBtn = this.querySelector('button[type="submit"]');
                    const originalText = submitBtn.innerHTML;
                    submitBtn.innerHTML = '<span class="spinner-border spinner-border-sm" role="status" aria-hidden="true"></span> 登录中...';
                    submitBtn.disabled = true;

                    // 模拟网络延迟，实际应用中不需要这个
                    setTimeout(function() {
                        submitBtn.innerHTML = originalText;
                        submitBtn.disabled = false;
                    }, 1500);
                }
            });

            // 自动清除错误信息
            usernameInput.addEventListener('input', function() {
                if (usernameError.textContent) {
                    usernameError.textContent = '';
                }
            });

            passwordInput.addEventListener('input', function() {
                if (passwordError.textContent) {
                    passwordError.textContent = '';
                }
            });

            // 如果有错误，添加动画效果
            const errorAlerts = document.querySelectorAll('.alert');
            errorAlerts.forEach(alert => {
                alert.classList.add('animate__animated', 'animate__headShake');
            });
        });
    </script>
</body>
</html>